/**
 * Created by wuyunan on 2017/5/16.
 */
import { Linking, NativeModules } from "react-native";

import { isIOS } from "../common";

const { RNBridgeModule } = NativeModules;

const LinkingUtil = {
  callPhone: phoneNumber => {
    if (isIOS) {
      const url = `telprompt:${phoneNumber}`;
      Linking.canOpenURL(url)
        .then(supported => {
          if (!supported) {
            console.log(`Can't handle url: ${url}`);
          } else {
            Linking.openURL(url).catch(err => {
              if (url.includes("telprompt")) {
                console.warn("openURL error", err);
              } else {
                console.warn("openURL error", err);
              }
            });
          }
        })
        .catch(err => console.warn("An unexpected error happened", err));
    } else {
      RNBridgeModule.preStartCall(phoneNumber);
    }
  },
};

export default LinkingUtil;
